package com.networkviwer.utils;

import com.networkviwer.networkviewer.NetworkViewer;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class UnionDatasourceUtil {

    static public Connection getConnection() throws ClassNotFoundException, SQLException {
        String url = "jdbc:h2:./database";
        String driverClass = "org.h2.Driver";
        Class.forName(driverClass);
        return DriverManager.getConnection(url);
    }

    static public void createTable() throws SQLException, ClassNotFoundException {
        Connection connectionNet = NetstatsDatasourceUtil.getConnection();
        Statement statementNet = connectionNet.createStatement();
        NetstatsDatasourceUtil.createTable(statementNet);
        statementNet.close();
        connectionNet.close();
        Connection connectionTask = TasklistDatasourceUtil.getConnection();
        Statement statementTask  = connectionTask.createStatement();
        TasklistDatasourceUtil.createTable(statementTask);
        statementTask.close();
        connectionTask.close();
        Connection connectionProcess = ProcessDatasourceUtil.getConnection();
        Statement statementProcess  = connectionProcess.createStatement();
        ProcessDatasourceUtil.createTable(statementProcess);
        statementProcess.close();
        connectionProcess.close();
    }

    static public ResultSet getAllData(Statement statement, Map<String,String> valueMap) throws SQLException {
        String sql = "select " +
                        "ns.protocol," +
                        "ns.local_address," +
                        "ns.external_address," +
                        "ns.status," +
                        "ns.pid," +
                        "tl.image," +
                        "tl.session," +
                        "tl.session_num," +
                        "tl.memory_usage " +
                     "from " +
                        "netstats ns," +
                        "tasklist tl " +
                     "where " +
                        "ns.pid = tl.pid ";
        if (NetworkViewer.tcpBtn.getStatus() == 0){
            sql += "and ns.protocol != 'TCP' ";
        }
        if (NetworkViewer.udpBtn.getStatus() == 0){
            sql += "and ns.protocol != 'UDP' ";
        }
        if (!valueMap.get("local_address").equals("")){
            sql += "and ns.local_address like '%" + valueMap.get("local_address") + "%'";
        }
        if (!valueMap.get("pid").equals("")){
            sql += "and ns.pid like '%" + valueMap.get("pid") + "%'";
        }
        if (!valueMap.get("image").equals("")){
            sql += "and tl.image like '%" + valueMap.get("image") + "%'";
        }
        if (NetworkViewer.pidOrder == 1){
            sql += " order by ns.pid ASC ";
        }else if (NetworkViewer.pidOrder == -1){
            sql += " order by ns.pid DESC ";
        }
        if (NetworkViewer.imageOder == 1){
            sql += " order by tl.image ASC ";
        }else if (NetworkViewer.imageOder == -1){
            sql += " order by tl.image DESC ";
        }
        return statement.executeQuery(sql);
    }

    /**
     * 根据pid查询文件路径
     */
    static public String getProcessPath(String pid) throws SQLException, ClassNotFoundException {
        Map<String,String> valueMap = new HashMap<>();
        valueMap.put("pid",pid);
        Connection connection = ProcessDatasourceUtil.getConnection();
        Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet resultSet = ProcessDatasourceUtil.getAllData(statement,valueMap);
        resultSet.next();
        String path = resultSet.getString("executablePath");
        // 关闭链接
        resultSet.close();
        statement.close();
        connection.close();
        return path;
    }

}
